package com.yqkj.zysoft.upms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.easy.common.sequencer.SnowflakeIdWorker;
import  com.project.easy.common.util.collection.CollectionUtil;
import  com.project.easy.common.util.constants.CommonConstant;
import  com.project.easy.common.util.enums.DelEnums;
import  com.project.easy.common.util.enums.StatusEnums;
import  com.project.easy.common.util.exception.business.BusinessException;
import  com.project.easy.common.util.string.StringUtil;
import  com.project.easy.common.util.tree.TreeUtil;
import com.yqkj.zysoft.upms.entity.FrameworkDepart;
import com.yqkj.zysoft.upms.entity.dto.DepartTreeResponse;
import com.yqkj.zysoft.upms.entity.dto.FrameWorkDepartAddRequest;
import com.yqkj.zysoft.upms.entity.dto.FrameWorkDepartUpdateRequest;
import com.yqkj.zysoft.upms.entity.dto.QueryDepartRequest;
import com.yqkj.zysoft.upms.mapper.FrameworkDepartMapper;
import com.yqkj.zysoft.upms.service.IFrameworkDepartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author GreatorMan.cool
 * @since 2022-09-13
 */
@Service
public class FrameworkDepartServiceImpl extends ServiceImpl<FrameworkDepartMapper, FrameworkDepart> implements IFrameworkDepartService {


    @Autowired
    private FrameworkDepartMapper frameworkDepartMapper;
    /**
     * 新增
     *
     * @param frameWorkDepartAddRequest 参数
     * @return 返回 true 成功 false 失败
     */
    @Override
    public Long add(FrameWorkDepartAddRequest frameWorkDepartAddRequest) {
        String id = SnowflakeIdWorker.nextIdStr();
        Long parentId = CommonConstant.ROOT_ID;
        if (StringUtil.isNotBlank(frameWorkDepartAddRequest.getParentCode())) {
            QueryWrapper<FrameworkDepart> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("inner_code", frameWorkDepartAddRequest.getParentCode());
            FrameworkDepart frameworkDepart = this.frameworkDepartMapper.selectOne(queryWrapper);
            if (Objects.isNull(frameworkDepart)) {
                throw  new BusinessException("上级部门非法");
            }
            parentId = frameworkDepart.getId();
            id = String.format("%s|%s", frameworkDepart.getInnerCode(), id);
        }

        FrameworkDepart frameworkDepart = new FrameworkDepart();
        BeanUtils.copyProperties(frameWorkDepartAddRequest, frameworkDepart);
        frameworkDepart.setInnerCode(id);
        frameworkDepart.setParentId(parentId);
        boolean save = this.save(frameworkDepart);
        if (save) {
            return frameworkDepart.getId();
        }
        throw  new BusinessException("存储失败");
    }

    /**
     * 更新数据
     *
     * @param frameWorkDepartUpdateRequest 参数
     * @return true 成功 false 失败
     */
    @Override
    public Boolean updateObject(FrameWorkDepartUpdateRequest frameWorkDepartUpdateRequest) {
        FrameworkDepart frameworkDepart = this.getFrameworkDepart(frameWorkDepartUpdateRequest.getId());
        frameworkDepart.setEmail(frameWorkDepartUpdateRequest.getEmail());
        frameworkDepart.setName(frameWorkDepartUpdateRequest.getName());
        frameworkDepart.setLeadName(frameWorkDepartUpdateRequest.getLeadName());
        frameworkDepart.setPhone(frameWorkDepartUpdateRequest.getPhone());
        frameworkDepart.setSort(frameWorkDepartUpdateRequest.getSort());

        return  this.updateById(frameworkDepart);
    }

    /**
     * 管理列表查询
     *
     * @param queryDepartRequest 分页查询条件
     * @return 返回数据
     */
    @Override
    public List<DepartTreeResponse> tree(QueryDepartRequest queryDepartRequest) {
        QueryWrapper<FrameworkDepart> queryWrapper = new QueryWrapper<>();
        Boolean isTree = Boolean.TRUE;
        if (StringUtil.isNotBlank(queryDepartRequest.getName())) {
            isTree = Boolean.FALSE;
            queryWrapper.eq("name", queryDepartRequest.getName());
        }
        if (StringUtil.isNotBlank(queryDepartRequest.getStatus())) {
            queryWrapper.eq("status", queryDepartRequest.getStatus());
            isTree = Boolean.FALSE;
        }
        queryWrapper.eq("has_del", DelEnums.NOT_DEL.getCode());
        List<FrameworkDepart> frameworkDeparts = this.frameworkDepartMapper.selectList(queryWrapper);
        if (!CollectionUtil.isNull(frameworkDeparts)) {
            List<DepartTreeResponse>  result = new ArrayList<>(CollectionUtil.listSize(frameworkDeparts));
            for (FrameworkDepart frameworkDepart : frameworkDeparts) {
                DepartTreeResponse departTreeResponse = new DepartTreeResponse();
                BeanUtils.copyProperties(frameworkDepart, departTreeResponse);
                departTreeResponse.setpId(frameworkDepart.getParentId());
                departTreeResponse.setName(frameworkDepart.getName());
                departTreeResponse.setId(frameworkDepart.getId());
                departTreeResponse.setValue(frameworkDepart.getInnerCode());
                departTreeResponse.setLabel(frameworkDepart.getName());
                result.add(departTreeResponse);
            }
            if (isTree) {
                return  TreeUtil.bulid(result, CommonConstant.ROOT_ID);
            } else {
                return result;
            }
        }
        return CommonConstant.EMPTY_LIST;
    }

    /**
     * 删除部门
     *
     * @param id ID
     * @return 返回 True 成功 false 失败
     */
    @Override
    public Boolean del(Long id) {
        FrameworkDepart frameworkDepart = getFrameworkDepart(id);
        if (frameworkDepart.getHasDel()) {
            throw  new BusinessException("已经删除");
        }
        String innerCode = frameworkDepart.getInnerCode();
        QueryWrapper<FrameworkDepart> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("inner_code", String.format("%s|", innerCode));
        queryWrapper.eq("has_del", DelEnums.NOT_DEL.getCode());
        List<FrameworkDepart> frameworkDeparts = this.frameworkDepartMapper.selectList(queryWrapper);
        if (!CollectionUtil.isNull(frameworkDeparts)) {
            throw  new BusinessException("下级部门有数据不能删除!");
        }
        frameworkDepart.setHasDel(Boolean.TRUE);
        this.frameworkDepartMapper.updateById(frameworkDepart);
        return Boolean.TRUE;
    }

    /**
     * 根据部门ID 获取部门数据
     * @param id ID
     * @return 返回部门数据
     */
    private FrameworkDepart getFrameworkDepart(Long id) {
        if (Objects.isNull(id)) {
            throw  new BusinessException("ID不能为空");
        }
        FrameworkDepart frameworkDepart = this.frameworkDepartMapper.selectById(id);
        if (Objects.isNull(frameworkDepart)) {
            throw  new BusinessException("ID非法");
        }
        return frameworkDepart;
    }

    /**
     * 启用
     *
     * @param id ID
     * @return true 启用成功 false 启用失败
     */
    @Override
    public Boolean able(Long id) {
        FrameworkDepart frameworkDepart = this.getFrameworkDepart(id);

        if (frameworkDepart.getStatus()) {
            throw  new BusinessException("启用不能再次启用!");
        }
        frameworkDepart.setStatus(Boolean.TRUE);
        this.baseMapper.updateById(frameworkDepart);
        return Boolean.TRUE;
    }

    /**
     *
     *
     * @param id ID
     * @return true 启用成功 false 启用失败
     */
    @Override
    public Boolean disAble(Long id) {
        FrameworkDepart frameworkDepart = this.getFrameworkDepart(id);
        QueryWrapper<FrameworkDepart> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("inner_code", String.format("%s|", frameworkDepart.getInnerCode()));
        queryWrapper.eq("has_del", DelEnums.NOT_DEL.getCode());
        queryWrapper.eq("status", StatusEnums.ENBATL.getCode());
        List<FrameworkDepart> frameworkDeparts = this.frameworkDepartMapper.selectList(queryWrapper);
        if (!CollectionUtil.isNull(frameworkDeparts)) {
            throw  new BusinessException("下级部门有启用的数据不禁用当前数据!");
        }
        frameworkDepart.setStatus(Boolean.FALSE);
        this.baseMapper.updateById(frameworkDepart);
        return Boolean.TRUE;
    }
}
